import anylise.ScanFolderAnalyzerBasic;
import anylise.TypeFolderAnalyzer;
import anylise.TypeInfoTreeAnalyzer;
import anylise.TypeSingleAnalyzer;
import com.google.gson.Gson;
import configMeta.Config;
import configMeta.ConfigFolderListMeta;
import output.OutputResConfig;
import utils.FileUtils;

import java.util.ArrayList;

/**
 * Created by eran on 14-9-7.
 */
public class ScanFolderToJson
{


    private Config mConfig;

    private OutputResConfig mOutputConfig;

    private ArrayList<ScanFolderAnalyzerBasic> mAllAnalyzerArray;

    private Boolean mIsReleaseModel;

    public void initialize(Config _config, Boolean _isReleaseModel)
    {
        mConfig = _config;
        mOutputConfig = new OutputResConfig();
        mIsReleaseModel = _isReleaseModel;

        mAllAnalyzerArray = new ArrayList<ScanFolderAnalyzerBasic>();
        tryToPushAnalyzerToArray(new TypeInfoTreeAnalyzer());
        tryToPushAnalyzerToArray(new TypeFolderAnalyzer());
        tryToPushAnalyzerToArray(new TypeSingleAnalyzer());

    }

    public void execute()
    {
        for (ConfigFolderListMeta folderMeta : mConfig.folderList)
        {
            Boolean isCanAnalyze = false;
            for (ScanFolderAnalyzerBasic analyzer : mAllAnalyzerArray)
            {
                if (analyzer.isCanAnyliseFolder(folderMeta.type))
                {
                    isCanAnalyze = true;
                    analyzer.analysisFolder(folderMeta);
                    break;
                }
            }
            if (!isCanAnalyze)
            {
                System.err.println("Unknown folder type: " + folderMeta.type);
            }
        }

        // ==============
        // ==生成JSON
        // ==============
        Gson gson = new Gson();
        String json = gson.toJson(mOutputConfig);
        FileUtils.writeStringToFile(json, mConfig.exportPath);
        System.out.println("Save file to : " + mConfig.exportPath + "\n");
    }


    private void tryToPushAnalyzerToArray(ScanFolderAnalyzerBasic _analyzer)
    {
        _analyzer.setIsReleaseModel(mIsReleaseModel);
        _analyzer.setOutPutConfig(mOutputConfig);
        mAllAnalyzerArray.add(_analyzer);
    }

}
